/*
 * Date          Author         Note
 * 2015.14.22    舞者无罪                   动作指令
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <mh/com.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "act.h"
#include "com.h"

/*
 *
 */
static void CreateActInterface(HWND hWnd)
{
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_FALSE,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP - 10,
        8,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
     CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_TRUE,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2 - 15,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);

    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc[16] = {
            GetString(564), GetString(565), GetString(566), GetString(567),
            GetString(568), GetString(569), GetString(570), GetString(571),
            GetString(572), GetString(573), GetString(574), GetString(575),
            GetString(576), GetString(577), GetString(578), GetString(579),
    };
    int i = 0;
    for(i = 0; i < 16; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            20 + (i >> 3) * 140,
            60 + (i & 7) * 30,
            100,
            20,
            hWnd, data.act.des == i ? MH_EDIT_BORDER_GREY : 0);
    }
}
/*
 *
 */
static int GetActIndex(uchar act)
{
    int ret = 0;
    switch(act)
    {
    case MH_ACT_TRANSFER:
        ret = 3;
        break;
    case MH_ACT_RECEIVE:
        ret = 10;
        break;
    case MH_ACT_TRAN_RECV:
        ret = 7;
        break;
    case MH_ACT_KNIT_RECV:
        ret = 8;
        break;
    case MH_ACT_KNIT:
        ret = 1;
        break;
    case MH_ACT_TUCK:
        ret = 9;
        break;
    case MH_ACT_RUDE:
        ret = 12;
        break;
    case MH_ACT_KNIT_RUDE:
        ret = 4;
        break;
    case MH_ACT_KNIT_TUCK:
        ret = 2;
        break;
    case MH_ACT_KNIT_TRAN:
        ret = 11;
        break;
    case MH_ACT_2RECV:
        ret = 15;
        break;
    case MH_ACT_TRAN_2RECV:
        ret = 14;
        break;
    case MH_ACT_2TUCK:
        ret = 13;
        break;
    case MH_ACT_KNIT_2TUCK:
        ret = 6;
        break;
    case MH_ACT_JI:
        ret = 5;
        break;
    }
    return ret;
}

static uchar GetActOfIndex(int index)
{
    uchar act = 0;
    switch(index)
    {
    case 1:
        act = MH_ACT_KNIT;
        break;
    case 2:
        act = MH_ACT_KNIT_TUCK;
        break;
    case 3:
        act = MH_ACT_TRANSFER;
        break;
    case 4:
        act = MH_ACT_KNIT_RUDE;
        break;
    case 5:
        act = MH_ACT_JI;
        break;
    case 6:
        act = MH_ACT_KNIT_2TUCK;
        break;
    case 7:
        act = MH_ACT_TRAN_RECV;
        break;
    case 8:
        act = MH_ACT_KNIT_RECV;
        break;
    case 9:
        act = MH_ACT_TUCK;
        break;
    case 10:
        act = MH_ACT_RECEIVE;
        break;
    case 11:
        act = MH_ACT_KNIT_TRAN;
        break;
    case 12:
        act = MH_ACT_RUDE;
        break;
    case 13:
        act = MH_ACT_2TUCK;
        break;
    case 14:
        act = MH_ACT_TRAN_2RECV;
        break;
    case 15:
        act = MH_ACT_2RECV;
        break;
    }
    return act;
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 16)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        int index = nID - IDC_EDIT1;
        if(data.act.des != index)
        {
            HWND hTmp = GetDlgItem(hWnd, data.act.des + IDC_EDIT1);
            SetEditBorder(hTmp, 0);
            InvalidateRect(hTmp, NULL, TRUE);
            hTmp = GetDlgItem(hWnd, nID);
            SetEditBorder(hTmp, MH_EDIT_BORDER_GREY);
            InvalidateRect(hTmp, NULL, TRUE);
            data.act.des = index;
            SetWindowAdditionalData(hWnd, data.value);
        }
    }
    else if(nID == ID_FALSE)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID == ID_TRUE)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.act.src != data.act.des)
        {
            printf("des          :%d\n",data.act.des);
            uchar act = GetActOfIndex(data.act.des);
            ushort nID = 0;
            HWND hTmp = GetHosting(hWnd);
            if(hTmp > 0)
            {
                hTmp = GetFocusChild(hTmp);
            }
            if(hTmp > 0)
            {
                nID = GetDlgCtrlID(hTmp);
            }
            if(nID > 0)
            {
                printf("sss:act:%x, nid:%d\n",act, nID);
                SendMessage(GetHosting(hWnd), MSG_SETDATA, (act << 16) | nID, 0);
            }
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, ID_TRUE);
    }
}
/*
 *
 */
static int ActWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateActInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateActWindow(HWND hHosting, uchar act)
{
    WND_EXTRA_DATA data;
    int index = GetActIndex(act);
    data.act.src = index;
    data.act.des = data.act.src;
    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(563);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ActWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd > 0)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + index));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
